<style lang="less">
@import './login.less';
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in" title="欢迎登录" :bordered="false">
        <div class="form-con">
          <login-form @on-success-valid="handleSubmit"></login-form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import LoginForm from '_c/login-form'
import { mapActions } from 'vuex'
import rcRequest from 'rc-request'
import jsCookie from 'js-cookie';

export default {
  components: {
    LoginForm
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'getUserInfo'
    ]),
    async handleSubmit({ userName, password }) {
      await this.handleLogin({ userName, password });
      await this.getUserInfo();
      const { data: { data: [result], code, msg } } = await rcRequest({
        method: 'post',
        url: '/login/select_login_user_info',
        data: {
          account: userName,
          password,
        },
      });
      if (Number(code) !== 200) {
        this.$Message.error(msg);
        return;
      }
      jsCookie.set('user_id', result.id);
      jsCookie.set('agent_id', result.agent_id);
      this.$router.push({ name: this.$config.homeName });
    }
  }
}
</script>

<style scoped></style>
